/*
*	(*^。^*) TFrameWork License (*^。^*)
*/

/*
 *  Title :  
 *          
 *  Description :  
 *          
 *          
 *  Date : 
 *
 *  Author : DengPing
 *
 *  Modify : 
 */

using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
using Object = UnityEngine.Object;

namespace TFrameWork.Module.ActionEditor
{
    public class GameObjectTrackStyle : CommonTrackStyle<GameObjectTrackStyle, GameObjectEventItemStyle>
    {
        public static Color particleColor = new Color(0.49f, 0.83f, 0.98f, 1f);

        protected override void InitStyle()
        {
            Top.style.borderLeftColor = particleColor;
            TrackMenuList.style.borderLeftColor = particleColor;
            Texture image = EditorGUIUtility.IconContent("d_Prefab Icon").image;
            StyleBackground style = new StyleBackground(image as Texture2D);
            Icon.style.backgroundImage = style;

        }

        public override CommonChildTrackBase CreateChildTrack()
        {
            return new CommonChildTrack();
        }

        public class CommonChildTrack : CommonChildTrackBase
        {
            public override void InitEvent()
            {
                if (_data == null || _data.events == null) return;

                for (int i = 0; i < _data.events.Count; i++)
                {
                    var e = _data.events[i] as PrefabEvent;
                    var eventItemStyle = new GameObjectEventItemStyle();
                    eventItemStyle.Init(this, e);
                    _eventItemStyleList.Add(eventItemStyle);
                }
            }

            protected override void DragObjCreateEvent(Object obj, int selectFrameIndex)
            {
                if (obj is GameObject prefab)
                {
                    if (prefab.GetComponentInChildren<ParticleSystem>())
                    {
                        CreatePrefabEvent(prefab, selectFrameIndex, typeof(ParticlePrefabEvent),true);
                    }
                    else
                    {
                        CreatePrefabEvent(prefab, selectFrameIndex, typeof(PrefabEvent),true);
                    }
                }
                else
                {
                    Debug.LogError("请拖拽有效的预制体资源");
                }
            }

            private List<Type> eventTypes = new List<Type>();

            private void InitTypes()
            {
                eventTypes.Clear();

                var types = typeof(PrefabBaseEvent).Assembly.GetTypes();
                for (int i = 0; i < types.Length; i++)
                {
                    var type = types[i];
                    if (!type.IsAbstract && type.IsSubclassOf(typeof(PrefabBaseEvent)))
                    {
                        eventTypes.Add(type);
                    }
                }
            }

            protected override void InitTrackMouseMene(VisualElement trackParent)
            {
                InitTypes();
                // // 创建右键菜单
                var menu = new ContextualMenuManipulator(evt =>
                {
                    for (int i = 0; i < eventTypes.Count; i++)
                    {
                        var type = eventTypes[i];
                        var attr = type.GetCustomAttribute(typeof(ShowCreateNameAttribute)) as ShowCreateNameAttribute;
                        string showname = type.Name;
                        if (attr != null)
                        {
                            showname = attr.name;
                        }

                        evt.menu.AppendAction(showname, p => { AddPrefab(p, type); }, addState);
                    }

                    addState = DropdownMenuAction.Status.Normal;
                });

                // 将右键菜单添加到 trackRoot 上
                trackParent.AddManipulator(menu);
            }

            private GameObject particleObject;

            public void AddPrefab(DropdownMenuAction obj, Type t)
            {
                ActionEditor.Instance.OpenObjectPicker<GameObject>("t:GameObject", go =>
                {
                    if (go != null && go is GameObject particle)
                    {
                        var mousePos = trackParent.WorldToLocal(obj.eventInfo.mousePosition);
                        int mouseIndex =
                            Mathf.RoundToInt(mousePos.x / ActionEditor.Instance.actionEditorConfig.frameUnitWidth);
                        CreatePrefabEvent(particle, mouseIndex, t,false);
                    }
                });
            }

            private void CreatePrefabEvent(GameObject prefab, int startIndex, Type t,bool isDrag)
            {

                var prefabEvent = DataBase.New<PrefabBaseEvent>(t);
                prefabEvent.prefab = prefab;
                prefabEvent.prefabPath = AssetDatabase.GetAssetPath(prefab);
                prefabEvent.durationIndex = prefabEvent.GetDefaultDuration();
                prefabEvent.startIndex = startIndex;
                var eventItemStyle = new GameObjectEventItemStyle();
                eventItemStyle.Init(this, prefabEvent);
                if (!isDrag)
                {
                    TUndo.RegisterCreatedObjectUndo(prefabEvent, "CreatePrefabEvent");
                    TUndo.RecordObject(_data, "CreatePrefabEvent");
                }

                _data.AddEvent(prefabEvent);
                AssetDatabase.AddObjectToAsset(prefabEvent, _data);
                EditorUtility.SetDirty(_data);
                AssetDatabase.SaveAssets();

                _eventItemStyleList.Add(eventItemStyle);
            }
        }
    }
}
